package com.bsc.rtc.lib.libraries.wedgit.loading;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.bsc.rtc.lib.libraries.utils.WindowUtil;

public class LoadingView extends View {
    String tag = "LoadingView";
    Paint paint;
    Rect rect;
    RectF rectF;
    RectF innerRectF;
    int defaultLeftPosition = 20;
    int defaultTopPositon = 20;
    int defaultRightPosition = 80;
    int defaultBottomPosition = 80;
    float defaultCircleX = 20;
    float defaultCircleY = 20;
    float startAngle = 0;
    float endAngle = 300;
    int radius = 10;
    Matrix matrix;
    Path path;

    public LoadingView(Context context) {
        super(context);
        init(context);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        paint = new Paint();
        matrix = new Matrix();
        paint.setColor(Color.BLUE);
        path = new Path();
        rect = new Rect(defaultLeftPosition, defaultTopPositon, defaultRightPosition, defaultBottomPosition);
        defaultCircleX = WindowUtil.getDispalyWidth(context);
        defaultCircleY =WindowUtil.getDispalyHeight(context);
        float outLeft=defaultCircleX/2-80;
        float outTop=defaultCircleY/2-80;
        float outButtom=defaultCircleY/2+80;
        float outRight=defaultCircleX/2+80;
        float inLeft=defaultCircleX/2-40;
        float inTop=defaultCircleY/2-40;
        float inButtom=defaultCircleY/2+40;
        float inRight=defaultCircleX/2+40;
        rectF = new RectF(outLeft, outTop, outRight, outButtom);
        innerRectF=new RectF(inLeft,inTop,inRight,inButtom);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        Log.i(tag, "width height="+width+" "+height);
        switch (MeasureSpec.getMode(widthMeasureSpec)) {
            case MeasureSpec.AT_MOST:
                Log.i(tag, "MeasureSpec.AT_MOST");
                break;
            case MeasureSpec.EXACTLY:
                Log.i(tag, "MeasureSpec.EXACTLY");
                break;
            case MeasureSpec.UNSPECIFIED:
                Log.i(tag, "MeasureSpec.UNSPECIFIED");
                break;
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        realDraw(canvas);
    }

    private void realDraw(Canvas canvas) {
        Log.i("zzw","realDraw");
        paint.setStrokeWidth(10);
        paint.setColor(Color.YELLOW);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(rect, paint);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        // canvas.drawCircle(defaultCircleX, defaultCircleY, radius, paint);
        // path.addCircle(defaultCircleX, defaultCircleY, radius, Path.Direction.CW);
        canvas.drawArc(rectF, startAngle, endAngle, false, paint);
        paint.setColor(Color.RED);
        canvas.drawArc(innerRectF, startAngle+180, endAngle+150, false, paint);
        //canvas.drawPath(path, paint);
    }
    public void startRotating(){
        ValueAnimator valueAnimator=ValueAnimator.ofFloat(0,360);
        valueAnimator.setDuration(10000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float x= (float) animation.getAnimatedValue();
                Log.i("zzw","x=="+x);
                startAngle+=x;
                if (startAngle>=360){
                    startAngle-=360;
                }
                endAngle+=x;
                if (endAngle>=360){
                    endAngle-=360;
                }
                postInvalidate();
            }
        });
        valueAnimator.start();
    }
}
